package chen;

import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

public class ImgUpload {
    public  static  void upload(String filename,HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        String username = new String();;
        Map<String,String> userName = (Map<String, String>) request.getSession().getAttribute("usermessage");
        if(userName != null)
        {
            //String usermessage = new String();
            for(String key : userName.keySet()){
                String value = userName.get(key);
                if (key.equals("username"))
                    username = value;
            }
        }
        else
        {
            response.sendRedirect("/error.html");
        }

        //修改头像
        //清除原先的session
        FileInputStream file = null;
        BufferedReader in = null;
        StringBuffer buf = new StringBuffer();
        request.getSession().removeAttribute("userName");
        request.getSession().removeAttribute("token");
        //读取文件中的用户数据
        try {
            file = new FileInputStream("src/main/User.txt");
            in = new BufferedReader(new InputStreamReader(file));
            String usermessage = null;
            Jsonb jsonb = JsonbBuilder.create();
            while ((usermessage = in.readLine()) != null) {
                User2 userDTO = jsonb.fromJson(usermessage, User2.class);
                if(userDTO.getUsername().equals(username))
                {
                    HttpSession session = request.getSession(true);
                    Map<String,String> hashmap = new HashMap<String,String>();
                    hashmap.put("username",userDTO.getUsername());
                    hashmap.put("password",userDTO.getPassword());
                    hashmap.put("email",userDTO.getEmail());
                    hashmap.put("sno",userDTO.getSno());
                    hashmap.put("img",userDTO.getImg());
                    hashmap.put("name",userDTO.getName());

                    session.setAttribute("usermessage", hashmap);
                    session.setAttribute("token", "not null");
                    User2 MessageDTO = new User2(username,userDTO.getEmail(),userDTO.getPassword(),userDTO.getSno(),filename,userDTO.getName());
                    String str = jsonb.toJson(MessageDTO);
                    buf.append(str + '\n');
                }
                else
                    buf.append(usermessage + '\n');
            }
            file.close();
            in.close();
        } catch (IOException e) {
            response.sendRedirect("/error.html");
        }

        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/main/User.txt")));
            System.out.println(buf.toString());
            out.write(buf.toString());
            out.close();
        } catch (IOException e) {
            response.sendRedirect("/error.html");
        }
        request.getSession().removeAttribute("userName");
        request.getSession().removeAttribute("token");
        response.sendRedirect("/index.jsp");
    }
}
